<template>
  <div>
    <!-- 面包屑导航栏 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!--  卡片区域-->

    <el-card>
      <!-- 添加分类按钮 -->
      <el-row>
        <el-col>
          <el-button type="primary" @click="addCate()">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 树形表格组件 -->
      <tree-table
        :data="goodsCateList"
        :columns="columns"
        border
        :show-index="true"
        :selection-type="false"
        :expand-type="false"
        :show-row-hover="false"
      >
        <!-- 是否有效-->
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted===false" style="color:lightgreen;"></i>
          <i class="el-icon-error" v-else></i>
        </template>
        <!-- 排序 -->
        <template slot="sort" slot-scope="scope">
          <el-tag type="success" v-if="scope.row.cat_level===0" size="mini">一级</el-tag>
          <el-tag type="primary" v-else-if="scope.row.cat_level===1" size="mini">二级</el-tag>
          <el-tag type="warning" v-else size="mini">三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opration" slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-setting"
            size="mini"
            @click="editCate(scope.row)"
          >编辑</el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            @click="deleteCate(scope.row)"
            size="mini"
          >删除</el-button>
        </template>
      </tree-table>
      <!-- 底部导航栏区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 编辑商品分类对话框 -->
    <el-dialog title="编辑分类" :visible.sync="editCateDialogVisible" width="50%">
      <el-form
        ref="editCateFormRef"
        :model="editCateForm"
        label-width="80px"
        :rules="editCateFormRules"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="editCateForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetEditDialog()">取 消</el-button>
        <el-button type="primary" @click="AddEditCate()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加分类对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      width="50%"
      @close="addCateDialogClosed()"
    >
      <!-- form表单区域 -->
      <el-form
        ref="addCateDialogFormRef"
        :model="addCateDialogform"
        label-width="80px"
        :rules="addCateDialogformRules"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateDialogform.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <!-- 级联选择器 -->
          <el-cascader
            v-model="selectedKeys"
            :options="goodsCateList"
            :props="goodsCateListProps"
            @change="parentCateChange"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <!-- 按钮区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitAddCateForm()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      editCateForm: {
        cat_name: "",
      },
      addCateDialogVisible: false,

      queryInfo: {
        type: [1, 2, 3],
        pagenum: 1,
        pagesize: 5,
      },
      goodsCateList: [],
      total: 0,
      // 级联选择器配置对象
      goodsCateListProps: {
        checkStrictly: true,
        expandTrigger: "hover",
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },
      // 选中的id
      selectedKeys: [],
      // 编辑分类的id
      editsid: "",

      // 添加分类对话框表单
      addCateDialogform: {
        cat_pid: 0,
        cat_name: "",
        cat_level: 0,
      },
      editCateDialogVisible: false,
      editCateFormRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
      addCateDialogformRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
      columns: [
        {
          label: "商品分类",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          type: "template",
          template: "isok",
        },
        {
          label: "排序",
          type: "template",
          template: "sort",
        },
        {
          label: "操作",
          type: "template",
          template: "opration",
        },
      ],
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    // 提交添加分类
    submitAddCateForm() {
      // 预校验
      this.$refs.addCateDialogFormRef.validate(async (valid) => {
        if (!valid) return;
        // 发起网络请求
        const { data: res } = await this.$http.post(
          "categories",
          this.addCateDialogform
        );
        if (res.meta.status !== 200) {
          this.$message.error("创建分类失败！");
        }

        this.$message.success("创建分类成功！");
        console.log("创建分类响应数据：");
        console.log(res);
        this.addCateDialogVisible = false;
        this.getCateList();
      });
    },
    // 关闭添加分类对话框
    addCateDialogClosed() {
      this.$refs.addCateDialogFormRef.resetFields();
      this.selectedKeys = [];
      this.addCateDialogform.cat_level = 0;
      this.addCateDialogform.cat_pid = 0;
    },
    //parentCateChange
    parentCateChange() {
      console.log(this.selectedKeys);

      const k = this.selectedKeys.length;
      console.log(this.addCateDialogform);
      this.addCateDialogform.cat_pid = this.selectedKeys[k - 1];
      this.addCateDialogform.cat_level = k;
    },
    // 添加商品分类
    addCate() {
      this.addCateDialogVisible = true;
    },
    // 获取商品分类列表
    async getCateList() {
      console.log(this.queryInfo.pagesize);
      //   发送网络请求获取商品分类列表
      const { data: res } = await this.$http.get("categories", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200) {
        return this.$message.error("获取商品分类失败！");
      }
      this.total = res.data.total;
      this.goodsCateList = res.data.result;

      console.log("商品列表here：");
      console.log(this.goodsCateList);
    },
    // 重置编辑分类对话框
    resetEditDialog() {
      this.$refs.editCateFormRef.resetFields();
      this.editCateDialogVisible = false;
    },
    // 添加编辑分类
    AddEditCate() {
      // 预校验
      this.$refs.editCateFormRef.validate(async (valid) => {
        if (!valid) return;
        // 发起网络请求
        const { data: res } = await this.$http.put(
          "categories/" + this.editsid,
          this.editCateForm
        );
        this.editCateDialogVisible = false;
        this.getCateList();
      });
    },
    //编辑分类信息
    editCate(cate) {
      console.log("cate:");
      console.log(cate);
      this.editCateDialogVisible = true;
      this.editsid = cate.cat_id;
      this.editCateForm.cat_name = cate.cat_name;
    },
    // 删除商品分类
    async deleteCate(cate) {
      console.log(cate);
      // 发起网络请求删除商品分类
      const { data: res } = await this.$http.delete(
        "categories/" + cate.cat_id
      );
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg);
      }
      this.$message.success("删除商品分类成功");
      this.getCateList();
    },
    // 监控page-size的改变
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      console.log(this.queryInfo.pagesize);
      this.getCateList();
    },
    // 监控pagenum的改变
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      console.log(this.queryInfo.pagenum);
      this.getCateList();
    },
  },
};
</script>
<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
